"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 基础功能
Case Name   : gs_guc reload 方式设置subscription_conflict_resolution参数,设置无效值合理报错
Create At   : 2023/09/06
Owner       : opentestcase033
Description :
    1.设置为apply_remote,查询参数值
    2.设置为keep_local,查询参数值
    3.设置为默认值error,查询参数值
    4.设置无效值
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.报错
    5.成功
History     :
"""
import os
import unittest

from yat.test import macro
from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class PubSubclass(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.commsh_pub = CommonSH('PrimaryDbUser')
        self.pri_userdb_pub = Node(node='PrimaryDbUser')
        self.com_pub = Common()
        self.constant = Constant()
        self.param = 'subscription_conflict_resolution'
        self.param_values = ['apply_remote', 'keep_local', 'error']
        self.values = [1, "''", 'null', 'applyremote',
                       '*', "'(error,keep_local)'"]

    def test_pubsub(self):
        seq = 1
        for value in self.param_values:
            text = f'-----step{seq}:设置为{value},查询参数值;expect:成功-----'
            self.log.info(text)
            cmd = f'source {macro.DB_ENV_PATH};' \
                  f'gs_guc reload -N all -I all -c"{self.param}={value}"'
            self.log.info(cmd)
            res = self.com_pub.get_sh_result(self.pri_userdb_pub, cmd)
            self.assertIn(self.constant.GSGUC_SUCCESS_MSG, res, f"执行失败:{text}")
            sql_res = self.commsh_pub.execut_db_sql(f'show {self.param};')
            self.log.info(sql_res)
            self.assertIn(value, sql_res, f'执行失败:{text}')
            seq = seq + 1

        text = '-----step4:设置无效值;expect:报错-----'
        self.log.info(text)
        for value in self.values:
            res = self.commsh_pub.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.param}={value}')
            self.assertFalse(res, f'执行失败:{text}')

    def tearDown(self):
        text = '-----step5:清理环境;expect:成功-----'
        self.log.info(text)
        cur_value = self.commsh_pub.execut_db_sql(f'show {self.param};')
        self.log.info(cur_value)
        if 'error' not in cur_value:
            res = self.commsh_pub.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG, f'{self.param}=error')
            restart_res = self.commsh_pub.restart_db_cluster()
            self.assertTrue(res and restart_res, f"执行失败:{text}")
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
